Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

384
Visualizações
Calling to restart a "service" in Linux from Python script not working from within crontab entry

I have the following python test script (extracted from a much bigger .py file) which I'm trying to get working on an EC2 instance (haproxy OpsWorks instance).

There seems to be a major difference between the permissions applied when running as a cron entry versus running as a python script from the shell.

The closest I've found in any other posts is here but that's relating to shell scripts and, given that I'm running python OK, the paths should ( I hope ) be sufficiently configured for the cronjob.

Environment: - Python version: 2.6.8 - Environment: OpsWorks HAProxy instance, on EC2. - Running as user: root.

Script

import subprocess
import boto
import logging

if __name__ == '__main__':
    logging.getLogger().setLevel(boto.logging.INFO)
    command = ['service', 'haproxy', 'reload'];
    logging.info('Executing: %s' % command)
    #shell=FALSE for sudo to work.
    subprocess.call(command, shell=False)

I have alternatively tried calling the subprocess command using the following to no avail.

# subprocess.call("%s %s %s %s" % ('sudo', 'service', 'haproxy', 'reload'))

Output when run from the command line:

[root@lb1 ~]# python tester.py
INFO:root:Executing: ['service', 'haproxy', 'reload']
Reloading haproxy:
[root@lb1 ~]#

Crontab entry:

[root@lb1 ~]# crontab -l
*/1 * * * * python ~/tester.py > ~/testlog 2>&1

Output when run as a crontab command

[root@lb1 ~]# cat testlog
INFO:root:Executing: ['service', 'haproxy', 'reload']
Traceback (most recent call last):
  File "/root/tester.py", line 13, in <module>
    subprocess.call(command, shell=False)
  File "/usr/lib64/python2.6/subprocess.py", line 478, in call
    p = Popen(*popenargs, **kwargs)
  File "/usr/lib64/python2.6/subprocess.py", line 639, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.6/subprocess.py", line 1228, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

I'm confused as to why on earth it's throwing the "No Such File Or Directory" error!

Can anyone provide any pointers? Incidentally this is part of a much bigger EC2 integration piece - hence the boto import [it's a nice way to get logging in this instance]. It's running as root because, from browsing SO, it's bad practice to have a cron entry for a user account running with a sudo command.

Cheers,

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In subprocess set shell=True

subprocess.call(command, shell=True)

I think your problem will be solved but sudo problem may be arised.

over 4 years ago · Santiago Trujillo Relatório

0

It turns out that it is indeed path related.

Path Information for Cronjobs
Commands in submodules

To solve it I provided the following entries via crontab -e

[root@lb1 ~]# crontab -l
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Now my service restarts as expected. Yay!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda